﻿// ===============================================================================
// Project Name        :    Gfw.BLL.Basic.SystemUser
// Project Description :    
// ===============================================================================
// Class Name          :    SystemUserLogic
// Namespace:          :    Gfw.BLL.Basic.SystemUser
// Class Version       :    v1.0.0.0
// Class Description   :    
// CLR Version:        :    4.0.30319.17626
//Author               :    lif
// Machine             :    ADICO-PC
// Create Time         :    2012/9/9 16:16:41
// Update Time         :    2012/9/9 16:16:41
// ===============================================================================
// Copyright © lif 2012 . All rights reserved.
// ===============================================================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gfw.Models.Admin;
using Gfw.Repositories.Admin;
using Gfw.Repository;
using Gfw.Repository.Admin;
using Gfw.Web.Models.SystemUser;

namespace Gfw.BLL.Basic.SystemUser
{
    public static class SystemUserLogic
    {
        
        #region 用户角色
        public static List<UserRoleModel> GetUserRoles()
        {
            RolesRepository _roleRepository = new RolesRepository();
            var list = _roleRepository.GetAll();
            var result = new List<UserRoleModel>();
            foreach (var l in list)
            {
                result.Add(l.CopyTo<UserRoleModel>());
            }
            return result;
        }
        #endregion

        #region 用户查询
        public static IList<NewUserModel> QuerySystemUser(string departmentId)
        {
            SystemUserRepository _repository = new SystemUserRepository();
            var list = _repository.GetUserByDepartment(departmentId);
            var result = new List<NewUserModel>();
            foreach (var l in list)
            {
                result.Add(l.CopyTo<NewUserModel>());
            }
            return result;
        }
        #endregion

        #region 添加用户
        public static void SaveNewSystemUser(NewUserModel model, string departmentId)
        {
            SystemUserRepository _repository = new SystemUserRepository();

          
           
            Gfw.Models.Admin.UsrSystemuser user;
            user = model.CopyTo<UsrSystemuser>();

            if (_repository.RegisterCheck(user) == false)
            {
                //登录名称重复性监测
                throw new Exception("登录名不能重复！");
            }

            user.Password = EncryptAlgorithmHelper.Encrypt(user.Password, user.LoginName);
            user.SecondPassword = EncryptAlgorithmHelper.Encrypt(user.SecondPassword, user.LoginName);
            DepartmentRepository drep = new DepartmentRepository();
            var department = drep.Get(departmentId);
            user.Departmentid = department;
            UnitOfWork work = new UnitOfWork();

            _repository.Save(user, work);
            UserRoleRepository roleRep = new UserRoleRepository();
            foreach (var r in model.UserRoles)
            {
                if (r.IsCheck)
                    roleRep.SaveUserRoleRelation(user, r.Id, work);
            }
            work.Commit();
        }

        #endregion

        #region 修改用户

        public static NewUserModel GetUserModel(string userId)
        {
            SystemUserRepository _repository = new SystemUserRepository();
           var user =  _repository.Get(userId);
            var model = user.CopyTo<NewUserModel>();
            model.UserRoles = Gfw.BLL.Basic.SystemUser.SystemUserLogic.GetUserRoles();

            model.Password = "";
            model.SecondPassword = "";

            model.Departmentid = user.Departmentid.Id;
            foreach(var r in model.UserRoles)
            {
                if(user.UsrUserroleList.Where(c=>c.Roleid.Id == r.Id).Count()>0)
                {
                    r.IsCheck = true;
                }
            }
            return model;
        }

        public static void EditSystemUser(NewUserModel model)
        {
            SystemUserRepository _repository = new SystemUserRepository();



            Gfw.Models.Admin.UsrSystemuser user;
            user = model.CopyTo<UsrSystemuser>();



            user.Password = EncryptAlgorithmHelper.Encrypt(user.Password, user.LoginName);
            user.SecondPassword = EncryptAlgorithmHelper.Encrypt(user.SecondPassword, user.LoginName);

            DepartmentRepository drep = new DepartmentRepository();
            var department = drep.Get(model.Departmentid);
            user.Departmentid = department;
            UnitOfWork work = new UnitOfWork();

            _repository.Update(user, work);
            UserRoleRepository roleRep = new UserRoleRepository();
            roleRep.DeleteByUser(user.Id, work);

            foreach (var r in model.UserRoles)
            {
                if (r.IsCheck)
                    roleRep.SaveUserRoleRelation(user, r.Id, work);
            }
            work.Commit();
        }

        #endregion
    }
}
